//****************************************************************************//
//* CI7 Libreria grafica                                                     *//
//*	ci7_text.cpp       														 *//
//*	                                                                         *//
//* ci7.hddisseny.es                                                         *//
//* hddisseny@ci7.hddisseny.es                                               *//
//****************************************************************************//

#include "ci7.hpp"

/*  Clase::metodo( Parametros )

    Explicacion
*/
void CI7_Text::TextInit( void ) {
    if(TTF_Init()==-1) {
        printf("TTF_Init: %s\n", TTF_GetError());
        exit(2);
    }
}

void CI7_Text::TextLoad( const char * cchUri, int iSize ) {
    _ci7Int_TTFont = TTF_OpenFont ( cchUri, iSize);
    if(!_ci7Int_TTFont) {
        printf("TTF_OpenFont: %s\n", TTF_GetError());
        exit(2);
    }
}

void CI7_Text::TextDraw( const char * ccgText, int x, int y, CI7_Color cColor) {

    int cr = (int)cColor.r;
    int cg = (int)cColor.g;
    int cb = (int)cColor.b;
    int ca = (int)cColor.a;

    SDL_Color clrFg = { cr,cg,cb,ca }; // set colour to blue (or 'red' for BGRA)
    SDL_Surface *sText = TTF_RenderUTF8_Blended( _ci7Int_TTFont, ccgText, clrFg );
    GLuint texture = 0;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    int Mode = GL_RGB;
    if(sText->format->BytesPerPixel == 4) {
        Mode = GL_RGBA;
    }

    glLoadIdentity();
    glTexImage2D(GL_TEXTURE_2D, 0, Mode, sText->w, sText->h, 0, Mode,
                 GL_UNSIGNED_BYTE, sText->pixels);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glBindTexture(GL_TEXTURE_2D,texture);

    glBegin(GL_QUADS);
    {
        glTexCoord2i(0, 0);
        glVertex3f(x, y, 0);
        glTexCoord2i(1, 0);
        glVertex3f(x + sText->w, y, 0);
        glTexCoord2i(1, 1);
        glVertex3f(x + sText->w, y + sText->h, 0);
        glTexCoord2i(0, 1);
        glVertex3f(x, y + sText->h, 0);
    }
    glEnd();

    SDL_FreeSurface( sText );
    glDeleteTextures(1, &texture);
}

void CI7_Text::TextDraw( int ccgText, int x, int y, CI7_Color cColor) {

    int cr = (int)cColor.r;
    int cg = (int)cColor.g;
    int cb = (int)cColor.b;
    int ca = (int)cColor.a;
    char bText[255];
    sprintf( bText, "%i", ccgText );
    SDL_Color clrFg = { cr,cg,cb,ca }; // set colour to blue (or 'red' for BGRA)
    SDL_Surface *sText = TTF_RenderUTF8_Blended( _ci7Int_TTFont, bText, clrFg );
    GLuint texture = 0;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    int Mode = GL_RGB;
    if(sText->format->BytesPerPixel == 4) {
        Mode = GL_RGBA;
    }

    glLoadIdentity();
    glTexImage2D(GL_TEXTURE_2D, 0, Mode, sText->w, sText->h, 0, Mode,
                 GL_UNSIGNED_BYTE, sText->pixels);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glBindTexture(GL_TEXTURE_2D,texture);

    glBegin(GL_QUADS);
    {
        glTexCoord2i(0, 0);
        glVertex3f(x, y, 0);
        glTexCoord2i(1, 0);
        glVertex3f(x + sText->w, y, 0);
        glTexCoord2i(1, 1);
        glVertex3f(x + sText->w, y + sText->h, 0);
        glTexCoord2i(0, 1);
        glVertex3f(x, y + sText->h, 0);
    }
    glEnd();

    SDL_FreeSurface( sText );
    glDeleteTextures(1, &texture);
}

void CI7_Text::TextDraw( float ccgText, int x, int y, CI7_Color cColor) {

    int cr = (int)cColor.r;
    int cg = (int)cColor.g;
    int cb = (int)cColor.b;
    int ca = (int)cColor.a;
    char bText[255];
    sprintf( bText, "%f", ccgText );
    SDL_Color clrFg = { cr,cg,cb,ca }; // set colour to blue (or 'red' for BGRA)
    SDL_Surface *sText = TTF_RenderUTF8_Blended( _ci7Int_TTFont, bText, clrFg );
    GLuint texture = 0;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    int Mode = GL_RGB;
    if(sText->format->BytesPerPixel == 4) {
        Mode = GL_RGBA;
    }

    glLoadIdentity();
    glTexImage2D(GL_TEXTURE_2D, 0, Mode, sText->w, sText->h, 0, Mode,
                 GL_UNSIGNED_BYTE, sText->pixels);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glBindTexture(GL_TEXTURE_2D,texture);

    glBegin(GL_QUADS);
    {
        glTexCoord2i(0, 0);
        glVertex3f(x, y, 0);
        glTexCoord2i(1, 0);
        glVertex3f(x + sText->w, y, 0);
        glTexCoord2i(1, 1);
        glVertex3f(x + sText->w, y + sText->h, 0);
        glTexCoord2i(0, 1);
        glVertex3f(x, y + sText->h, 0);
    }
    glEnd();

    SDL_FreeSurface( sText );
    glDeleteTextures(1, &texture);
}

void CI7_Text::TextQuit ( void ) {
    TTF_CloseFont ( _ci7Int_TTFont );
}
